package com.example.demo.service;

import com.example.demo.dao.BookDescMapper;
import com.example.demo.dao.BookInfoMapper;
import com.example.demo.entity.BookDesc;
import com.example.demo.entity.BookInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.awt.print.Book;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class BookInfoServiceImpl implements  BookInfoService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    @Autowired
    private BookDescMapper bookDescMapper;

    @Override
    public List<BookInfo> selectAll() {

        Example example=new Example(BookInfo.class);

        Example.Criteria criteria=example.createCriteria();

        example.setOrderByClause("publish_date DESC,deal_mount DESC,look_mount DESC,store_time DESC");

        List<BookInfo> bookInfos=bookInfoMapper.selectByExample(example);

        return bookInfos;
    }

    @Override
   // @Cacheable(cacheNames = "book",key = " 'bookInfo_'+#id ")
    public BookInfo findByBookId(Integer id) {
        System.out.println("bookInfoMapper findByBookId fun started...");
        BookInfo bookInfo=bookInfoMapper.selectByPrimaryKey(id);
        return bookInfo;
    }

    @Override
    public List<BookInfo> selectByCatId(Integer id) {
        Example example=new Example(BookInfo.class);

        Example.Criteria criteria=example.createCriteria();

        criteria.andEqualTo("book_category_id",id);

        List<BookInfo> recommendBookList=bookInfoMapper.selectByExample(example);

        return recommendBookList;
    }

    @Override
    public PageInfo<BookInfo> findByCondition(int cate_id, String keywords, int page, int pageSize, int store_id) {

        //加分类Id查询条件,where (name like ? or author like ? or isbn like ?) and cateId = ?
        PageHelper.startPage(page,pageSize);

        Example example=new Example(BookInfo.class);

        if(!keywords.isEmpty()){
            String kwd="%"+keywords.trim()+"%";
            Example.Criteria criteria=example.createCriteria();
            criteria.orLike("name",kwd);
            criteria.orLike("author",kwd);
            criteria.orLike("isbn",kwd);
            criteria.orLike("press",kwd);
        }
        if(cate_id!=0){
            Example.Criteria catCriteria=example.createCriteria();
            catCriteria.andEqualTo("book_category_id",cate_id);
        }
        if(store_id!=0){

            Example.Criteria catCriteria1=example.createCriteria();
            catCriteria1.andEqualTo("store_id",store_id);
            catCriteria1.andEqualTo("is_shelf",1);
        }

        List<BookInfo> bookInfos=bookInfoMapper.selectByExample(example);
        PageInfo<BookInfo> bookInfoPageInfo=new PageInfo<>(bookInfos);
        return bookInfoPageInfo;
    }

    @Override
    public PageInfo<BookInfo> findByConditions(int cate_id, String keywords, int page, int pageSize, int store_id) {

        //加分类Id查询条件,where (name like ? or author like ? or isbn like ?) and cateId = ?
        PageHelper.startPage(page,pageSize);

        Example example=new Example(BookInfo.class);

        if(!keywords.isEmpty()){
            String kwd="%"+keywords.trim()+"%";
            Example.Criteria criteria=example.createCriteria();
            criteria.orLike("name",kwd);
            criteria.orLike("author",kwd);
            criteria.orLike("isbn",kwd);
            criteria.orLike("press",kwd);
        }
        if(cate_id!=0){
            Example.Criteria catCriteria=example.createCriteria();
            catCriteria.andEqualTo("book_category_id",cate_id);
        }
        if(store_id!=0){

            Example.Criteria catCriteria1=example.createCriteria();
            catCriteria1.andEqualTo("store_id",store_id);
           // catCriteria1.andEqualTo("is_shelf",1);
        }

        List<BookInfo> bookInfos=bookInfoMapper.selectByExample(example);
        PageInfo<BookInfo> bookInfoPageInfo=new PageInfo<>(bookInfos);
        return bookInfoPageInfo;
    }





    @Override
    public BookInfo findByIdAvailable(int book_id, int buyNum) {
//select * from book_info where book_id=? and is_shelf=1 and store_mount>buyNum
        Example example=new Example(BookInfo.class);
        Example.Criteria criteria=example.createCriteria();

        criteria.andEqualTo("book_id",book_id);
        criteria.andEqualTo("is_shelf",1);
        criteria.andGreaterThan("store_mount",0);

        List<BookInfo> bookInfos=bookInfoMapper.selectByExample(example);
        if(bookInfos!=null)
            return bookInfos.get(0);

        return null;
    }

    @Override
    public List<BookInfo> findByStoreId(Integer storeId) {

        Example example=new Example(BookInfo.class);
        Example.Criteria criteria=example.createCriteria();

        criteria.andEqualTo("store_id",storeId);

        List<BookInfo> bookInfo=bookInfoMapper.selectByExample(example);
        return bookInfo;
    }

    @Override
    public int insertBook(BookInfo bookInfo,String bookDesc) {


       // bookInfo.setIsbn(RandomStringUtils.randomNumeric(9));
        bookInfo.setDiscount(bookInfo.getPrice().divide(bookInfo.getMarket_price(),2, RoundingMode.HALF_UP).multiply(new BigDecimal(10.0)));
        bookInfo.setStore_time(new Date());
        bookInfo.setLook_mount(100);
        bookInfo.setDeal_mount(100);
        bookInfo.setPack_style("平装-胶订");

        bookInfo.setIs_shelf(1);
        //bookInfoMapper.insert(bookInfo);
        bookInfoMapper.insertSelective(bookInfo);


        BookDesc bookDesc1=new BookDesc();
        bookDesc1.setBook_id(bookInfo.getBook_id());
        bookDesc1.setBookDesc(bookDesc);
        bookDesc1.setCreated(new Date());
        bookDesc1.setUpdated(new Date());
        bookDescMapper.insert(bookDesc1);


        return 1;
    }

    @Override
    public int updateBookInfo(BookInfo bookInfo,String bookDesc) {
        bookInfo.setDiscount(bookInfo.getPrice().divide(bookInfo.getMarket_price(),2, RoundingMode.HALF_UP).multiply(new BigDecimal(10.0)));

        BookDesc bookDesc1=new BookDesc();

        bookDesc1.setBookDesc(bookDesc);
        bookDesc1.setBook_id(bookInfo.getBook_id());

        BookDesc find=bookDescMapper.selectByPrimaryKey(new Integer(bookInfo.getBook_id()));
        if(find==null){
            bookDesc1.setCreated(new Date());
            bookDesc1.setUpdated(new Date());
            bookDescMapper.insert(bookDesc1);

        }else{
            bookDesc1.setUpdated(new Date());

            bookDescMapper.updateByPrimaryKeySelective(bookDesc1);
        }


        return bookInfoMapper.updateByPrimaryKeySelective(bookInfo) ;
    }

    @Override
    public int changeShelfStatus(int bookId, int isShelf) {

        BookInfo bookInfo=new BookInfo();
        bookInfo.setBook_id(bookId);
        bookInfo.setIs_shelf(isShelf);
        return bookInfoMapper.updateByPrimaryKeySelective(bookInfo);
    }

    @Override
    public int deleteBookInfoByID(int bookId) {
        return bookInfoMapper.deleteByPrimaryKey(new Integer(bookId));
    }
}
